#include <iostream>
#include "base/pugixml.hpp"

using namespace std;

void BuildMap(const pugi::xml_node & root, const string & prefix) {
  pugi::xml_node node = root.first_child();
  while(node) {
    if (node.first_child()) {
      BuildMap(node, prefix.empty() ? node.name() : prefix + '.' + node.name());
    } else {
      cout << prefix << node.name() << " = " << node.value() << endl;
    }
    node = node.next_sibling();
  }
}
void demo1() {
  pugi::xml_document doc;
  string xml = "<f v=\"1.0\" type=\"blogPublish\">"
  "<time>1269246798271</time>"
  "<type>601</type>"
  "<origin>"
    "<type>1</type>"
    "<url>source.url</url>"
    "<title>fake title</title>"
    "<image>xxx.gif</image>"
  "</origin>"
  "<from>"
    "<id>232690766</id>"
    "<name>霍克明</name>"
    "<tinyimg>hd30/20071008/14/29/tiny_4723d170.jpg</tinyimg>"
  "</from>"

  "<blog>"
    "<id>453813878</id>"
    "<title>&lt;span&gt;比法律更厉害的东西，你猜是什么&lt;/span&gt;</title>"
    "<digest>&lt;span&gt;有人说，这个世界上没有比瘾更严重的东西了，比如有人有偷东西的瘾，明知道&lt;/span&gt;&lt;wbr /&gt;&lt;span class=&quot;word_break&quot;&gt;&lt;/span&gt;&lt;span&gt;要判刑，管不住自己的手。有人有暴露的瘾，见到单身的女人就把家伙亮出来，&lt;/span&gt;&lt;wbr /&gt;&lt;span class=&quot;word_break&quot;&gt;&lt;/span&gt;&lt;span&gt;明知道被逮住就是一阵胖揍。再比如，烟瘾，酒瘾。等等，烟瘾，酒瘾，还真干&lt;/span&gt;&lt;wbr /&gt;&lt;span class=&quot;word_break&quot;&gt;&lt;/span&gt;&lt;span&gt;不过一样东西，就是你的老婆。我本来不抽烟，后来我的领导关怀我，把我发展&lt;/span&gt;&lt;wbr /&gt;&lt;span class=&quot;word_break&quot;&gt;&lt;/span&gt;&lt;span&gt;起来了。抽烟。后来市场部门来了个领导，也抽烟。但是他们都有一特牛的特征&lt;/span&gt;&lt;wbr /&gt;&lt;span class=&quot;word_break&quot;&gt;&lt;/span&gt;&lt;span&gt;。快下班不...&lt;/span&gt;</digest>"
    "<url>http://blog.renren.com/GetEntry.do?id=453813878&amp;owner=232690766</url>"
  "</blog>"
"</f>";

  std::string xml2 = 
    "<root>"
    "<item id=\"23340225\">"
      "<qid>23340225</qid>"
      "<userName>安全无极限</userName>"
      "<nickName></nickName>"
      "<realName>阿强</realName>"
      "<loginEmail>zouguiqiang@360.cn</loginEmail>"
      "<email>zouguiqiang@360.cn</email>"
      "<status>0</status>"
      "<type>1</type>"
      "<src>360UC</src>"
      "<title>"
        "<i360safe>utitle=360杀毒产品经理|||ulevel=3|||uicon=star</i360safe>"
      "</title>"
      "<regtime>2009-06-28 09:41:55</regtime>"
      "<address></address>"
      "<telephone></telephone>"
      "<areacd></areacd>"
      "<sign></sign>"
      "<fav></fav>"
      "<postcode></postcode>"
      "<birthday></birthday>"
      "<imageId>2401f4dq21de</imageId>"
      "<imageUrl>http://u1.qhimg.com/qhimg/quc/48_48/24/01/f4/2401f4dq21de.0958e3.jpg</imageUrl>"
      "<intro></intro>"
      "<sex>未知</sex>"
      "<rule>"
        "<realName>1</realName>"
        "<intro>1</intro>"
        "<address>1</address>"
      "</rule>"
      "<ext></ext>"
      "<theme>360</theme>"
      "<lastmodifytime>2012-04-09 09:40:45</lastmodifytime>"
      "<basic_isopen></basic_isopen>"
      "<details_isopen></details_isopen>"
      "<bloodtype>A</bloodtype>"
      "<education>0</education>"
      "<job>0</job>"
      "<married>0</married>"
      "<interest></interest><homepage></homepage><msn></msn><qq></qq><weibo></weibo><birthland></birthland>"
      "<school></school><certificate></certificate><company></company><income>0</income><bodytype>0</bodytype>"
      "<height>0</height><weight>0</weight><lovegametype></lovegametype><online></online>"
    "</item>"
    "</root>";

  if (doc.load(xml2.c_str())) {
    return 0;
  } else {
    std::cout<< "Error." << std::endl;
    return 0;
  }

  if (doc.load(xml.c_str())) {
    BuildMap(doc, "");
  }
}

void demo2() {
  pugi::xml_document doc;
  pugi::xml_parse_result result = doc.load_file("user.xml");

  if (!result) {
    cout << "load error" << endl;
    return;
  }
  pugi::xml_node root = doc.child("root");
  
  for(pugi::xml_node item = root.child("item"); item; item = root.next_sibling("item")) {
    cout << "-------------" << item.child_value("qid") << endl;
  }

}

void demo3() {
  pugi::xml_document doc;
  pugi::xml_parse_result result = doc.load_file("xgconsole.xml");

  if (!result) {
    cout << "load error" << endl;
    return;
  }
  pugi::xml_node tools = doc.child("Profile").child("Tools");
  cout << tools.child_value("xx") << endl;
  if (!tools) {
    cout << "no tools node" << endl;
    return;
  }
  cout << "++++++++" << endl;
  
  for(pugi::xml_node tool = tools.child("Tool"); tool; tool = tool.next_sibling("Tool")) {
    cout << "-------------" << tool.child_value("qid") << endl;
  }
}

int demo4() {
    pugi::xml_document doc;
    if (!doc.load_file("xgconsole.xml")) return -1;

    pugi::xml_node tools = doc.child("Profile").child("Tools");

    //[code_traverse_base_basic
    for (pugi::xml_node tool = tools.first_child(); tool; tool = tool.next_sibling())
    {
        std::cout << "Tool:";

        for (pugi::xml_attribute attr = tool.first_attribute(); attr; attr = attr.next_attribute())
        {
            std::cout << " " << attr.name() << "=" << attr.value();
        }

        std::cout << std::endl;
    }
    //]

    std::cout << std::endl;

    //[code_traverse_base_data
    for (pugi::xml_node tool = tools.child("Tool"); tool; tool = tool.next_sibling("Tool"))
    {
        std::cout << "Tool " << tool.attribute("Filename").value();
        std::cout << ": AllowRemote " << tool.attribute("AllowRemote").as_bool();
        std::cout << ", Timeout " << tool.attribute("Timeout").as_int();
        std::cout << ", Description '" << tool.child_value("Description") << "'\n";
    }
    //]

    std::cout << std::endl;

    //[code_traverse_base_contents
    std::cout << "Tool for *.dae generation: " << tools.find_child_by_attribute("Tool", "OutputFileMasks", "*.dae").attribute("Filename").value() << "\n";

    for (pugi::xml_node tool = tools.child("Tool"); tool; tool = tool.next_sibling("Tool"))
    {
        std::cout << "Tool " << tool.attribute("Filename").value() << "\n";
    }
    //]
}

int main(){
  demo3();
  return 0;
}

